const rules: { [key: string]: RegExp } = {
	phone: /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/,
	float2: /^(([0-9]{1})|([1-9]{1}[0-9]{1,}))(\.{1}[0-9]{1,2})?$/
}
/**
 * @description public资源
 * @return string
 */
export function publicUrl(url: string | undefined): string {
	if (url === undefined) return ``;
	if (url.slice(0, 1) !== '/') url = '/' + url;
	// if (substr($url, 0, 1) !== '/')  $url  = '/' . $url;
	return import.meta.env.VITE_BASE_URL + url;


}


/*价格保留两位小数*/
export function twoFixed(value: number | string = 0.00): string {
	if (value === undefined) return '0.00';
	value = (Math.round(parseFloat(value.toString()) * 100) / 100).toString()
	let s = value.split('.')
	// console.log('s :>> ', s)
	// 只有整数
	if (s.length === 1) {
		value = value + '.00'
		return value
	}
	// 有小数
	if (s.length > 1) {
		// 只有一位小数
		if (s[1].length < 2) {
			value = value + '0'
		} else {
			value = s[0] + '.' + s[1].substring(0, 2)
		}
		return value
	}
	return '0.00';
}

/**
 * file 转Base64 DataURL
 * @param {File} file
 * @returns
 */
export function fileToBase64Async(file: File) {
	return new Promise<string>((resolve, reject) => {
		let reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = (e: any) => {
			resolve(e.target.result);
		};
	});
}


/**
 * @description:  检查验证规则
 * @param {string} key
 * @return {*}
 */
export function validateRule(key: string): RegExp {

	return rules[key];
}


interface TagColor {

	color: string;
	border: string;
	backgroundColor: string;

}
export function tagColor(index: number): TagColor {
	const color: Array<TagColor> = [
		{
			'color': '#409eff',
			'border': '1px solid #d9ecff',
			'backgroundColor': '#ecf5ff',
		},
		{
			'color': '#67c23a',
			'border': '1px solid #e1f3d8',
			'backgroundColor': '#f0f9eb',
		},
		{
			'color': '#e6a23c',
			'border': '1px solid #faecd8',
			'backgroundColor': '#fdf6ec',
		},
		{
			'color': '#f56c6c',
			'border': '1px solid #fde2e2',
			'backgroundColor': '#fde2e2',
		}
	];
	if (index > color.length - 1) index = index % color.length;
	return color[index];
}

/**
 * @description:     下载链接
 * @param {string} href
 * @param {string} text 文件名称
 * @return {*}
 */
export function aDownLoad(href: string, text?: string) {

	let type = href.split('/').pop()?.split('.').pop();

	if (type) {
		var bqa = document.createElement("a");
		//给a标签的href属性赋值
		bqa.setAttribute("href", href);
		//给a标签的download属性赋值,表示下载的文件名

		bqa.setAttribute("download", text ? text + "." + type : ""); //文件类型

		//调用a标签的点击事件
		bqa.click();
		//移除a标签
		bqa.remove()
	}

}
/**
 * @description:  div 补够一行
 * @param {number} len
 * @param {number} numebr
 * @return {*}
 */
export function surplusDiv(len: number, numebr: number): number {
	return len <= numebr ? numebr - len : len - (len % numebr)
}
/**
 * @description:   数组随机打乱顺序
 * @param  array
 * @return {*}
 */
export function shuffle<T>(array: Array<T>): Array<T> {
	var copy: Array<T> = [],
		n = array.length,
		i;
	// 如果还剩有元素。。
	while (n) {
		// 随机选取一个元素
		i = Math.floor(Math.random() * n--);
		// 移动到新数组中
		copy.push(array.splice(i, 1)[0]);
	}
	return copy;
}

/**
 * @description: 深拷贝
 * @param {any} obj
 * @return {*}
 */
export function copyObj(obj: any = {}, callBack: undefined | ((newObj: any) => {}) = undefined) {
	//变量先置空
	let newobj: any = null;
	//判断是否需要继续进行递归
	if (typeof obj == "object" && obj !== null) {
		if (obj instanceof Array) {
			newobj = []; //进行下一层递归克隆
		} else {
			newobj = {};
			obj = callBack === undefined ? obj : callBack(obj)
		}
		for (var i in obj) {
			newobj[i] = copyObj(obj[i], callBack);
		} //如果不是对象直接赋值

	} else {
		newobj = obj;
	}
	return newobj;
}

/**
 * @description:          数字补0
 * @param {number} num    购买人数
 * @return {*}
 */
export function payNumEscape(num: number): string {
	if (num <= 99) return num.toString();
	else {
		let fillZero: string = '';
		for (let i = 0; i < num.toString().length - 1; i++) {
			fillZero += '0';
		}
		return num.toString().substring(0, 1) + fillZero + '+';
	}
}


export function jumpGoodsUrl(type: string, query: { [key: string]: string | number }): string {
	switch (type) {
		case 'spot':
			return '/mtm/spot/index/spotDetail';
		case 'fabric':
			return '/mtm/fabric/index/fabricDetail';
		case 'mtm':
		default:
			return '/mtm/index'
	}
}

